package com.samp.tools.parsii;

import parsii.eval.Expression;
import parsii.eval.Parser;
import parsii.eval.Scope;
import parsii.eval.Variable;
import parsii.tokenizer.ParseException;

public class ParsiiDemo {
	
	private static void calBM25(Double weightValue, Double freq, Double norm) throws ParseException{
		String expr = "weightValue * freq / (freq + norm)";
		Scope scope = new Scope(); 
		//Expression似乎不能缓存，它会保存之前的变量值。
		Expression express = Parser.parse(expr, scope);
		Variable weightVar = scope.getVariable("weightValue");
		Variable freqVar = scope.getVariable("freq");
		Variable normVar = scope.getVariable("norm");
		weightVar.setValue(weightValue);
		freqVar.setValue(freq);
		normVar.setValue(norm);
		System.out.println("bm25 result="+express.evaluate());
	}

	private static void calBM25_tfNorm( Double freq, Double fieldLength,  Double avgFieldLength, Double k1,  Double b) throws ParseException{
		String expr = "(freq * (k1 + 1)) / (freq + k1 * (1 - b + b * fieldLength / avgFieldLength))";
		Scope scope = new Scope(); 
		//Expression似乎不能缓存，它会保存之前的变量值。
		Expression express = Parser.parse(expr, scope);
		Variable fieldLengthVar = scope.getVariable("fieldLength");
		Variable freqVar = scope.getVariable("freq");
		Variable avgFieldLengthVar = scope.getVariable("avgFieldLength");
		Variable k1Var = scope.getVariable("k1");
		Variable bVar = scope.getVariable("b");
		fieldLengthVar.setValue(fieldLength);
		freqVar.setValue(freq);
		avgFieldLengthVar.setValue(avgFieldLength);
		k1Var.setValue(k1);
		bVar.setValue(b);
		System.out.println("calBM25_tfNorm result="+express.evaluate());
		
	}
	
	private static void logInfo(){
		float score = 1*1.0f / 17;
		System.out.println(score);
		for( int i = 0 ; i < 100 ; i++){
			System.out.println(i+"  Math.log(i)="+Math.log10(i));
			
		}
	}
	
	public static void main(String[] args) {
		try {
			//calBM25(0.2697251,1.0,0.6826713);
			calBM25_tfNorm(1.0, 10.24, 24.083334, 1.2, 0.75);
			//logInfo();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
